<?php

namespace App\Service;


use App\Service\BaseService;
use Hyperf\DbConnection\Db;

class AdvertService extends BaseService
{

    public $params = [];

    public static function advert($descryptData,$appId)
    {
        $userData = self::getUserInfoByUid($descryptData['uid']);
        $plan = $userData['plan'];
        $channel = $userData['channel'];
        $unionSite = $userData['union_site'];
        $regDays = self::userRegDays($userData);
        $isNuser = $regDays==1?1:0;
        $post = $descryptData;
        $uid = $descryptData['uid'];

        $adCpm = self::getCpmById($post['aid']);
        $aidShy = round($adCpm/1000,4);

//        if($post['event_type'] == 1){
//            $redis = RedisService::getInstance();
//
//            if($isNuser){
//                $regInc = $userData['reg_inc']+$aidShy;
//                if($aidShy>0){
//                    $userNewData = [
//                        'id' => $uid,
//                        'reg_inc' => $regInc,
//                        'ltv' => $regInc
//                    ];
//                    Db::table('users')->update($userNewData);
//                }
//                if($plan){
//                    if(!$userData['ocean_active']){
//                        $this->huichuan($regInc);
//                    }
//                    if(!$userData['crux_action']){
//                        $this->cruxAction($adCpm,$regInc);
//                    }
//                }
//            }else{
//                $ltv = $userData['ltv']+$aidShy;
//                if($aidShy>0){
//                    $userNewData = [
//                        'id' => $uid,
//                        'ltv' => $ltv
//                    ];
//                    if($regDays == 2){
//                        $inc2 = $userData['inc2']+$aidShy;
//                        $userNewData['inc2'] = $inc2;
//                    }
//                    if($regDays == 3){
//                        $inc3 = $userData['inc3']+$aidShy;
//                        $userNewData['inc3'] = $inc3;
//                    }
//                    Db::table('users')->update($userNewData);
//                }
//            }
//
//            if($isNuser){
//                $this->huichuanTongji($channel,'view_num1');
//                $plan && $this->huichuanTongji($plan,'view_num1');
//                $unionSite && $this->huichuanTongji($unionSite,'view_num1');
//
//                if($aidShy>0){
//                    $this->huichuanTongji($channel,'reg_inc',$aidShy);
//                    $plan && $this->huichuanTongji($plan,'reg_inc',$aidShy);
//                    $unionSite && $this->huichuanTongji($unionSite,'reg_inc',$aidShy);
//                }
//
//                if($post['small_type'] == '开屏'){
//                    $this->huichuanTongji($channel,'kaiping_num');
//                    $plan && $this->huichuanTongji($plan,'kaiping_num');
//                    $unionSite && $this->huichuanTongji($unionSite,'kaiping_num');
//
//                    if($aidShy>0){
//                        $this->huichuanTongji($channel,'kaiping_inc',$aidShy);
//                        $plan && $this->huichuanTongji($plan,'kaiping_inc',$aidShy);
//                        $unionSite && $this->huichuanTongji($unionSite,'kaiping_inc',$aidShy);
//                    }
//                }
//            }
//            $this->huichuanTongji($channel,'view_num2');
//            $plan && $this->huichuanTongji($plan,'view_num2');
//            $unionSite && $this->huichuanTongji($unionSite,'view_num2');
//
//            if($aidShy>0){
//                $this->huichuanTongji($channel,'active_inc',$aidShy);
//                $plan && $this->huichuanTongji($plan,'active_inc',$aidShy);
//                $unionSite && $this->huichuanTongji($unionSite,'active_inc',$aidShy);
//            }
//
//            $isVideo = in_array($post['small_type'],['全屏视频','激励视频']);
//            if($isVideo){
//                if($isNuser){
//                    $userVideoTable = $userData['app_id'].'_nuser_video_'.$uid;
//                    $userVideoNum = $redis->get($userVideoTable);
//                    if(!$userVideoNum){
//                        $userVideoNum = 1;
//                        $this->huichuanTongji($channel,'valide_num1');
//                        $plan && $this->huichuanTongji($plan,'valide_num1');
//                        $unionSite && $this->huichuanTongji($unionSite,'valide_num1');
//                        $redis->set($userVideoTable,$userVideoNum,86400);
//                    }else if($userVideoNum==1){
//                        $userVideoNum = 2;
//                        $this->huichuanTongji($channel,'valide_num2');
//                        $plan && $this->huichuanTongji($plan,'valide_num2');
//                        $unionSite && $this->huichuanTongji($unionSite,'valide_num2');
//                        $redis->set($userVideoTable,$userVideoNum,86400);
//                    }
//                    $this->huichuanTongji($channel,'nuser_video_num');
//                    $plan && $this->huichuanTongji($plan,'nuser_video_num');
//                    $unionSite && $this->huichuanTongji($unionSite,'nuser_video_num');
//                }
//                $this->huichuanTongji($channel,'video_num');
//                $plan && $this->huichuanTongji($plan,'video_num');
//                $unionSite && $this->huichuanTongji($unionSite,'video_num');
//            }
//
//            $day = date('Ymd');
//            $deuTable = $userData['app_id'].'_'.$uid.'_nuser_deu_'.$day;
//            if(!$redis->get($deuTable)){
//                $this->huichuanTongji($channel,'deu_num');
//                $plan && $this->huichuanTongji($plan,'deu_num');
//                $unionSite && $this->huichuanTongji($unionSite,'deu_num');
//                $redis->set($deuTable,1,24*3600);
//            }
//            $this->channelAd($post['aid'],$channel,$plan,$regDays);
//        }
        /*---------------------广告记录开始-----------------------*/
        $post['channel'] = $channel;
        $post['use_day'] = $regDays;
        $post['plan'] = $plan;
        $post['union_site'] = $unionSite;
        $post['cpm'] = $adCpm?:0;
        $post['aid_shy'] = $aidShy?:0;


        $rs = self::addAdRecord($post);
        /*---------------------广告记录结束-----------------------*/
        return $rs;
    }

    public static function getCpmById($aid){
        Db::table('rg_cpm')
            ->where(['aid'=>$aid])
            ->value('cpm');
    }


    //广告精简版记录表
    protected static function addAdRecord($post){
//        $isAdRecord = $this->isAdRecord($post['aid'],$post['event_type']);
//        if(!$isAdRecord){
//            return true;
//        }
        $advertData = [
            'uid' => $post['uid'],
            'aid' => $post['aid'],
            'big_type' => $post['big_type'],
            'small_type' => $post['small_type'],
            'event_type' => $post['event_type'],
            'app_id' => $post['app_id'],
            'channel' => !empty($post['channel'])?$post['channel']:0,
            'plan' => $post['plan']??"",
            
            'use_day' => $post['use_day'],
            'cpm' => $post['cpm'],
            'aid_shy' => $post['aid_shy'],
            'create_time' => time()
        ];

        $table = 'ad_record_20210819'; //= self::getDateTable('ad_record');


        try{
            $adId = Db::table($table)->insertGetId($advertData);
        }catch (\Exception $e){
            usleep(10000);
            $adId = Db::table($table)->insert($advertData);
        }

        if(!$adId){
            return false;
//            $this->returncode(300,'新增广告事件失败');
        }
        return true;
    }
}